package org.zizi.dao;

import java.util.List;

import org.zizi.modal.VatCredit;
import org.zizi.modal.VatCreditInvoice;
import org.zizi.util.PagedList;
import org.zizi.util.SqlBuilder;

public class VatCreditDaoImpl extends BaseDao implements VatCreditDao
{
	public long addOrUpdateVatCredit(VatCredit credit) throws Exception
	{
		if (credit.getId() > 0)
		{
			esql.helper().updatei(credit, "t_vat_credit", credit.getId());
			return credit.getId();
		}
		else
		{
			esql.helper().inserti(credit, "t_vat_credit");
			return getGeneratedId("t_vat_credit");
		}
	}
	
	//上期剩余未抵减额
	public double getPreviousLeftCredit(String identify, int year, int month) throws Exception
	{
		List<Double> list = esql.page(Double.class, 0, 1, "select this_left_credit from t_vat_credit where ((year=? and month<?) or (year<?)) and identify=? and valid=? order by year desc, month desc", year, month, year, identify, true);
		
		if (list == null || list.isEmpty()) return 0d;
		else return list.get(0);
	}
	
	public void deleteVatCredit(long id) throws Exception
	{
		esql.helper().delete("t_vat_credit_invoice", "vat_credit_id=?", id);
		esql.helper().deletei("t_vat_credit", id);
	}
	
	//看有没有更晚的有效清单
	public boolean existLatterCredit(String identify, int year, int month, Long exclude) throws Exception
	{
		SqlBuilder sb = new SqlBuilder("select * from t_vat_credit where identify=? and ((year=? and month>=?) or (year>?)) and valid=?", identify, year, month, year, true);
		if (exclude != null) sb.append(" and id <> ?", exclude);
		
		List<VatCredit> list = esql.list(VatCredit.class, sb.getSql(), sb.getParameters());
		
		return list != null && !list.isEmpty();
	}

	public VatCredit getVatCredit(long id) throws Exception
	{
		return esql.helper().queryi(VatCredit.class, "t_vat_credit", "id=?", id);
	}
	
	public VatCredit getVatCredit(String identify, int year, int month) throws Exception
	{
		return esql.helper().queryi(VatCredit.class, "t_vat_credit", "identify=? and year=? and month=?", identify, year, month);
	}

	//历史记录、无论是否废弃，都列出来
	public PagedList<VatCredit> listVatCredits(String identify, int beginYear, int beginMonth, int endYear, int endMonth, int offset, int limit) throws Exception
	{
		boolean valid = false;
		
		SqlBuilder sb = new SqlBuilder();
		if (identify != null) sb.append(true, "identify=?", identify);
		if (beginYear < endYear)
		{
			valid = true;
			sb.append(true, "((year=? and month>=?) or (year>? and year<?) or (year=? and month<=?))", beginYear, beginMonth, beginYear, endYear, endYear, endMonth);
		}
		else if ((beginYear == endYear) && (beginMonth <= endMonth))
		{
			valid = true;
			sb.append(true, "year=? and month>=? and month<=?", beginYear, beginMonth, endMonth);
		}
		
		if (!valid)
			return null;
		else
			return esql.helper().pagei(VatCredit.class, "t_vat_credit", offset, limit, "submit_time desc", sb.getSql(), sb.getParameters());
	}

	public void updateVatCreditInvoices(long creditId, List<VatCreditInvoice> invoices) throws Exception
	{
		//删除原来存在的
		esql.helper().delete("t_vat_credit_invoice", "vat_credit_id=?", creditId);
		//添加新的
		for (int i = 0; i < invoices.size(); i++)
		{
			VatCreditInvoice vci = invoices.get(i);
			vci.setVatCreditId(creditId);
			vci.setPlace(i + 1);
			
			esql.helper().insert(vci, "t_vat_credit_invoice", null, null);
		}
	}

	public List<VatCreditInvoice> getVatCreditInvoices(long creditId) throws Exception
	{
		return esql.helper().listi(VatCreditInvoice.class, "t_vat_credit_invoice", "place", "vat_credit_id=?", creditId);
	}

	//检查发票代码和号码组合是否被使用过
	public VatCreditInvoice getVatCreditInvoiceByCodeAndNum(String code, String number, long exclude) throws Exception
	{
		return esql.helper().query(VatCreditInvoice.class, "t_vat_credit vc, t_vat_credit_invoice vci", "vci.*", null, "vc.id=vci.vat_credit_id and vc.valid=? and vci.invoice_code=? and vci.invoice_num=? and vc.id<>?", true, code, number, exclude);
	}

	//增值税专用发票，每种类型一辈子只能填写一次
	public VatCreditInvoice getSepVatCreditInvoice(String identify, long typeId, long exclude) throws Exception
	{
		return esql.helper().query(VatCreditInvoice.class, "t_vat_credit vc, t_vat_credit_invoice vci", "vci.*", null, "vc.id=vci.vat_credit_id and vc.valid=? and vci.invoice_category=? and vci.invoice_type_id=? and vc.id<>? and vc.identify=?", true, VatCreditInvoice.SEP, typeId, exclude, identify);
	}

	//普通发票，每种类型一年只能填写一次
	public VatCreditInvoice getManOrFixVatCreditInvoice(String identify, long typeId, int year, long exclude) throws Exception
	{
		return esql.helper().query(VatCreditInvoice.class, "t_vat_credit vc, t_vat_credit_invoice vci", "vci.*", null, "vc.id=vci.vat_credit_id and vc.valid=? and (vci.invoice_category=? or vci.invoice_category=?) and vci.invoice_type_id=? and vc.id<>? and vc.identify=? and vc.year=?", true, VatCreditInvoice.MAN, VatCreditInvoice.FIX, typeId, exclude, identify, year);
	}
}
